const db=require('../utils/mysql')
const { v4: uuidv4 } = require('uuid');
module.exports={
    partsAdd:async (ctx,next)=>{
        let query=ctx.request.body
        console.log(query)
        let partsId=uuidv4()
        let result= await new Promise((resolve,reject)=>{
            const sql=`INSERT INTO parts (partsId,partsname,partsnumber,entrytime,content,partsstatus) 
            values ('${partsId}','${query.partsname}',${query.partsnumber},'${query.entrytime}','${query.content}','${query.partsstatus=='正常'?1:0}')`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'配件添加成功',
                    status:200
                })
            })
        })
        ctx.body=result
    },
    partsEdit:async (ctx,next)=>{
        let query=ctx.request.body
        console.log(query)
        let result= await new Promise((resolve,reject)=>{
            const sql=`update parts set 
            partsname='${query.partsname}',partsnumber='${query.partsnumber}',entrytime='${query.entrytime}',content='${query.content}',partsstatus='${query.partsstatus=='正常'?1:0}'
            where partsId='${query.partsId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'配件修改成功',
                    status:200
                })
            })
        })
        ctx.body=result
    },
    partsDelete:async (ctx,next)=>{
        let query=ctx.request.query
        let result= await new Promise((resolve,reject)=>{
            const sql=`delete from parts where partsId='${query.partsId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'配件删除成功',
                    status:200
                })
            })
        })
        ctx.body=result
    },
    getPartsList:async (ctx,next)=>{
        let result= await new Promise((resolve,reject)=>{
            const sql=`select * from parts`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'配件列表获取成功',
                    status:200,
                    data:data
                })
            })
        })
        ctx.body=result

    },
    getPartsInfo:async (ctx,next)=>{
        let query=ctx.request.query
        let result= await new Promise((resolve,reject)=>{
            const sql=`select * from parts where partsId='${query.partsId}'`
            db(sql,(err,data)=>{
                if(err) reject(err);
                resolve({
                    msg:'配件详情获取成功',
                    status:200,
                    data:data
                })
            })
        })
        ctx.body=result

    }
}

